package ro.gateway.aida.struts.action;

import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserGroup;
import ro.gateway.aida.usr.UserGroupDB;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.Utils;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class ViewGroupsAction extends TilesAction {

    /**
     * Method execute
     * @param ActionMapping mapping
     * @param ActionForm form
     * @param HttpServletRequest request
     * @param HttpServletResponse response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
			PersistenceToken token = PersistenceManager.
							tokenLookup(sc );
            UserGroupDB ugdb = UserGroupDB.getManager(token);
            String act = request.getParameter("act");
            String id = request.getParameter("id");
            
            //setting default group
			int dgId=Integer.parseInt(Utils.getProperty( token, "defaultGroup" ));
			UserGroup dg=ugdb.getById(dgId);
			request.setAttribute("defaultGroup",dg);
			
            User user = UserUtils.getLogged(request, response);


            if (user == null)
                return mapping.findForward("plslogin");

            Arbiter ar = Arbiter.getManager(token);

            if (!ar.canDo(request, Perms.ADMIN_TOOLS)) {
                HttpSession session = request.getSession();
                session.invalidate();

                return mapping.findForward("norights");
            }

            if ("confirm".equals(act)) {
                request.setAttribute("id", id);
                request.setAttribute("act", "delete");
				Utils.setBreadCrumbs(request,"bc.groups.confirmdelete");
                return mapping.findForward("confirmdelete");
            } else {
                //just create the group list
                Set groups = ugdb.createGroupList();

                request.setAttribute("groups", groups);
				Utils.setBreadCrumbs(request,"bc.groups.view");
                return mapping.findForward("viewgroups");
            }
        } catch (Exception e) {
            BugTracker.publishBug(e, request);

            return mapping.findForward("fail");
        }
    }
}
